package com.fun.feifun.modules.sys.auth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fun.feifun.base.result.Result;
import com.fun.feifun.modules.sys.auth.model.SysMenu;
import com.fun.feifun.modules.sys.auth.model.SysRole;
import com.fun.feifun.modules.sys.auth.model.dto.MenuTree;
import com.fun.feifun.modules.sys.auth.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* <p>
* 系统菜单 接口控制器
* </p>
*
* @author wkf
* @since 2022-04-21
*/
@RestController
@Slf4j
@Api(tags = "系统-菜单")
@RequestMapping("/sys/menu")
@SaCheckLogin()
public class SysMenuController {


    @Resource
    private ISysMenuService sysMenuService;


    @PostMapping(value = "/save")
    @ApiOperation(value = "添加或者修改")
    public Result save(@Validated @RequestBody SysMenu item) {
        if( sysMenuService.saveItem(item)){
            return Result.success();
        }
        return Result.err();
    }

    @PostMapping(value = "/removes")
    @ApiOperation(value = "删除")
    public  Result removes(@Validated @RequestBody List<Long> ids) {
        QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().in(SysMenu::getParentId,ids);
        int count = sysMenuService.count(queryWrapper);
        if(count>0){
            return Result.err("请先删除下级菜单");
        }
        sysMenuService.removeByIds(ids);
        return Result.success();
    }


    @GetMapping("/findById")
    @ApiOperation(value = "根据ID查找数据")
    public Result findById(@Validated @RequestParam("id") Long id){
        SysMenu item = sysMenuService.getById(id);
        if(item==null){
            return Result.err("尚未查询到此ID");
        }
        return Result.success(item);
    }


    @GetMapping("/all")
    @ApiOperation(value = "获取全部数据")
    public Result< List<SysMenu>> getAll(){
        List<SysMenu> all = sysMenuService.list();
        return Result.success(all);
    }


    @PostMapping("/page")
    @ApiOperation(value = "获取分页信息")
    public Result< Page<SysMenu>> page(
        @RequestParam(name = "page", defaultValue = "1") int index,
        @RequestParam(name = "size", defaultValue = "20") int size){
        Page page = new Page(index,size);
        QueryWrapper<SysMenu> wrapper=new QueryWrapper();
        wrapper.lambda().orderByAsc(SysMenu::getParentId).orderByAsc(SysMenu::getSort);
        sysMenuService.page(page, wrapper);
        return Result.success(page);
    }

    @PostMapping("/getTree")
    @ApiOperation(value = "获取树")
    public Result< List<MenuTree>> getTree(){
        List<MenuTree> allTree = sysMenuService.getAllTree();
        return Result.success(allTree);
    }
    @PostMapping("/getTreeByRoleId")
    @ApiOperation(value = "根据角色id获取菜单树")
    public Result<List<MenuTree>> getTreeByRoleId(@RequestParam(name = "roleId", defaultValue = "1") Long roleId){
        List<MenuTree> tree = sysMenuService.getTreeByRoleId(roleId);
        return Result.success(tree);
    }

    @PostMapping("/getListByRoleId")
    @ApiOperation(value = "根据角色id获取菜单列表")
    public Result<List<Long>> getListByRoleId(@RequestParam(name = "roleId", defaultValue = "1") Long roleId){
        List<Long> list = sysMenuService.getListByRoleId(roleId);
        return Result.success(list);
    }

    @PostMapping("/moveUp")
    @ApiOperation(value = "上移菜单")
    public Result moveUp(@Validated @RequestBody SysMenu item){
        sysMenuService.moveUp(item);
        return Result.success();
    }

    @PostMapping("/moveDown")
    @ApiOperation(value = "下移菜单")
    public Result moveDown(@Validated @RequestBody SysMenu item){
        sysMenuService.moveDown(item);
        return Result.success();
    }


}
